package com.example.demo.file.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.request.AlipayMarketingCardTemplateCreateRequest;
import com.alipay.api.request.AlipayOfflineMaterialImageUploadRequest;
import com.alipay.api.request.AlipayOpenAppQrcodeCreateRequest;
import com.alipay.api.response.AlipayMarketingCardTemplateCreateResponse;
import com.alipay.api.response.AlipayOfflineMaterialImageUploadResponse;
import com.alipay.api.response.AlipayOpenAppQrcodeCreateResponse;
import com.example.demo.file.Entity.MaterialEntity;
import com.example.demo.file.service.IAliMaterial;
import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

/**
 * Created at 2021/3/11 2:00 下午
 *
 * @author Alex
 * description
 */
@Service
@Slf4j
public class AliMaterialImpl implements IAliMaterial {
    @Autowired
    private DefaultAlipayClient alipayClient;

    @Autowired
    private Gson gson;

    @Override
    public boolean upload(MaterialEntity materialEntity) {
        AlipayOfflineMaterialImageUploadRequest request = new AlipayOfflineMaterialImageUploadRequest();
        request.setImageType(materialEntity.getImageType());
        request.setImageName(materialEntity.getImageName());
        FileItem ImageContent = new FileItem("/Users/asrock/Desktop/test.png");
        // 图片本地地址
        request.setImageContent(ImageContent);
        request.setImagePid(materialEntity.getImagePid());
        AlipayOfflineMaterialImageUploadResponse response = null;
        try {
            response = alipayClient.execute(request);
            log.info(gson.toJson(response));
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.error("阿里云上传物料失败，原因{}", e.getMessage());
            return false;
        }
        if (response.isSuccess()) {
            System.out.println("调用成功");
            return true;
        } else {
            System.out.println("调用失败");
            return false;
        }

    }

    @Override
    public boolean makeMemberTemplate() {
        AlipayMarketingCardTemplateCreateRequest request = new AlipayMarketingCardTemplateCreateRequest();

        request.putOtherTextParam("app_auth_token", null);
        request.setBizContent("");
        AlipayMarketingCardTemplateCreateResponse response = null;
        try {
            response = alipayClient.execute(request);
            log.error("创建会员卡模板成功，原因{}", gson.toJson(response));
            return true;
        } catch (AlipayApiException e) {
            log.error("创建会员卡模板失败，原因{}", e.getMessage());
            return false;
        }
    }

    /**
     * 苹果：page/activity/activity?cate_id=2&cate_child_id=4
     * 华为：page/activity/activity?cate_id=2&cate_child_id=5
     * 小米：page/activity/activity?cate_id=2&cate_child_id=16
     * VIVO: page/activity/activity?cate_id=2&cate_child_id=21
     * OPPO: page/activity/activity?cate_id=2&cate_child_id=22
     * 荣耀：page/activity/activity?cate_id=2&cate_child_id=17
     */

    @Override
    public boolean makeQrcode(String url) {
        AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
        request.setBizContent("{" +
                "\"url_param\":\"page/activity/activity\"," +
                "\"query_param\":\"cate_id=2&cate_child_id=4\"," +
                "\"describe\":\"苹果手机\"" +
                "  }");
        AlipayOpenAppQrcodeCreateResponse response = null;
        try {
            response = alipayClient.execute(request);
            log.info("二维码创建成功，url=={}", response.getQrCodeUrl());
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return false;
    }
}
